package pemks.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pemks.entity.File;
import pemks.pemks_function;
import pemks.util.DBUtil;
import swing.SmartGrid;

public class FileDAO {

    private pemks_function pf = SmartGrid.pf;
    // 保存文件密文
    public int saveFile(String owner, String cipherText, String keywords) {
        String sql = "INSERT INTO files (owner, cipher_text) VALUES (?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {

            pstmt.setString(1, owner);
            pstmt.setString(2, cipherText);
            pstmt.executeUpdate();

            ResultSet rs = pstmt.getGeneratedKeys();
            return rs.next() ? rs.getInt(1) : -1;
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    // 根据关键词搜索文件
    public List<File> searchByKeywords(String Tw) {
        List<File> files = new ArrayList<>();
        ResultSet rs = null;
        PreparedStatement pstmt = null;
        try (Connection conn = DBUtil.getConnection()) {

            String sql = "SELECT id, cipher_text, owner FROM files";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while(rs.next()) {
                String ct = rs.getString("cipher_text");
                Map<String, String> searchResult = pf.Search(
                        pf.bp, pf.G1, pf.G2, pf.GT, pf.Zq, ct, Tw
                );

                if("true".equals(searchResult.get("flag"))) {
                    int id=rs.getInt("id");
                    String owner=rs.getString("owner");
                    String cipherText=rs.getString("cipher_text");
                    File file = new File(id,owner,cipherText);
                    files.add(file);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return files;
    }

    // 根据ID获取文件
    public File getFileById(int id) {
        String sql = "SELECT * FROM files WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            ResultSet rs = pstmt.executeQuery();
            return rs.next() ? new File(id, rs.getString("owner"), rs.getString("cipher_text")) : null;
        } catch (SQLException e) {
            return null;
        }
    }

    // 删除文件（需验证所有者）
    public boolean deleteFile(int id, String owner) {
        String sql = "DELETE FROM files WHERE id = ? AND owner = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setInt(1, id);
            pstmt.setString(2, owner);
            return pstmt.executeUpdate() > 0;
        } catch (SQLException e) {
            return false;
        }
    }
}